/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Administracion.DAO;

import com.dp1.pucp.Modelo.Clases.Departamento;
import com.dp1.pucp.Modelo.Clases.Distrito;
import com.dp1.pucp.Modelo.Clases.Empleado;
import com.dp1.pucp.Modelo.Clases.Maquina;
import com.dp1.pucp.Modelo.Clases.Pedidocompra;
import com.dp1.pucp.Modelo.Clases.Provincia;
import com.dp1.pucp.Modelo.Clases.Superhombre;
import com.dp1.pucp.Modelo.Clases.Tipodocumento;
import com.dp1.pucp.Modelo.Clases.Tipomaquina;
import com.dp1.pucp.Util.HibernateUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author pachon
 */
public class RRHHDAO {

    public void guardarEmpleado(Empleado empleado) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            session.saveOrUpdate(empleado);
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
        }
    }

    public void guardarMaquinaria(Maquina maquinaria) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            session.saveOrUpdate(maquinaria);
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
        }
    }
    
    public ArrayList<String> listarEstadoEmpleado() {
        ArrayList<String> estados = new ArrayList<>();
        estados.add(0, "Seleccionar");
        estados.add(1, "Activo");
        estados.add(2, "No Activo");
        return estados;
    }
    

    public ArrayList<String> listarEstado() {
        ArrayList<String> estados = new ArrayList<>();
        estados.add(0, "Seleccionar");
        estados.add(1, "Utilizable");
        estados.add(2, "No Utilizable");
        estados.add(3, "En Mantenimiento");
        return estados;
    }

    public ArrayList<String> listarTurno() {
        ArrayList<String> estados = new ArrayList<>();
        estados.add("Mañana");
        estados.add("Tarde");
        estados.add("Noche");
        return estados;
    }

    public ArrayList<String> listarTipoEmpleado() {
        ArrayList<String> estados = new ArrayList<>();
        estados.add("Operario");
        estados.add("Administrativo");
        return estados;
    }

    public Empleado sacarUltCodPedido() {
        int cont = 0;
        ArrayList<Empleado> listaResultados = new ArrayList<>();

        //String lista=null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Empleado order by idempleado").list();
            Iterator iter = lista.iterator();

            while (iter.hasNext()) {
                Empleado p = (Empleado) iter.next();
                Hibernate.initialize(p.getCodigo());

                //Hibernate.initialize(p.getTipo());
                cont = cont + 1;
                listaResultados.add(p);
            }
            tx.commit();

        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            if (cont == 0) {
                Empleado empNuevo = new Empleado();
                empNuevo.setCodigo("EMP00000000");

                return empNuevo;
            } else {
                return (listaResultados.get(cont - 1));
            }
        }
    }

    public String llenaCodigo(Empleado e) {

        Empleado emp = sacarUltCodPedido();
        String codigo = emp.getCodigo();
        //aca saca el codigo mas grande  PED00000001
        if (e != null) {
            return codigo;
        }
        if (emp.getCodigo().equals("EMP00000000")) {
            return "EMP00000001";
        } else {
            String ceros = "";
            //Genero Codigo Siguiente
            String ped = codigo.substring(0, 3);
            String num = codigo.substring(3, 11);
            int longC = num.length(); //aca siempre es 8

            int numero = Integer.parseInt(num);
            numero++;

            String nume = String.valueOf(numero);
            //saco longitud de ese entero en caracteres
            int longE = nume.length();

            for (int i = 1; i < (longC - longE + 1); i++) {
                ceros = ceros + '0';
            }

            codigo = ped + "" + ceros + "" + numero;
            return codigo;
        }

    }

    public Maquina sacarUltCodMaquina() {
        int cont = 0;
        ArrayList<Maquina> listaResultados = new ArrayList<>();

        //String lista=null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Maquina order by idmaquina").list();
            Iterator iter = lista.iterator();

            while (iter.hasNext()) {
                Maquina p = (Maquina) iter.next();
                Hibernate.initialize(p.getCodigo());

                //Hibernate.initialize(p.getTipo());
                cont = cont + 1;
                listaResultados.add(p);
            }
            tx.commit();

        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            if (cont == 0) {
                Maquina empNuevo = new Maquina();
                empNuevo.setCodigo("MAQ00000000");

                return empNuevo;
            } else {
                return (listaResultados.get(cont - 1));
            }
        }
    }

    public String llenaCodigoMaquina(Maquina e) {

        Maquina emp = sacarUltCodMaquina();
        String codigo = emp.getCodigo();
        //aca saca el codigo mas grande  PED00000001
        if (e != null) {
            return codigo;
        }
        if (emp.getCodigo().equals("MAQ00000000")) {
            return "MAQ00000001";
        } else {
            String ceros = "";
            //Genero Codigo Siguiente
            String ped = codigo.substring(0, 3);
            String num = codigo.substring(3, 11);
            int longC = num.length(); //aca siempre es 8

            int numero = Integer.parseInt(num);
            numero++;

            String nume = String.valueOf(numero);
            //saco longitud de ese entero en caracteres
            int longE = nume.length();

            for (int i = 1; i < (longC - longE + 1); i++) {
                ceros = ceros + '0';
            }

            codigo = ped + "" + ceros + "" + numero;
            return codigo;
        }

    }

    public ArrayList<Tipodocumento> listarTipoDoc() {
        ArrayList<Tipodocumento> estados = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Tipodocumento order by idtipodocumento").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Tipodocumento p = (Tipodocumento) iter.next();
                Hibernate.initialize(p.getIdtipodocumento());
                Hibernate.initialize(p.getDescripcion());
                Hibernate.initialize(p.getDocumento());
                estados.add(p);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return estados;
        }
    }

    public ArrayList<Distrito> listarDistrito(Provincia p) {
        ArrayList<Distrito> distritos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Distrito where idprovincia= " + p.getIdprovincia() + " order by iddistrito").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Distrito d = (Distrito) iter.next();
                Hibernate.initialize(d.getIddistrito());
                Hibernate.initialize(d.getProvincia());
                distritos.add(d);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return distritos;
        }
    }

    public String sacaDescripcion(Tipomaquina p) {
        ArrayList<Tipomaquina> desc = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Tipomaquina where idtipomaquina= " + p.getIdtipomaquina() + " order by idtipomaquina").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Tipomaquina d = (Tipomaquina) iter.next();
                Hibernate.initialize(d.getIdtipomaquina());
                Hibernate.initialize(d.getDescripcion());
                desc.add(d);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return desc.get(0).getDescripcion();
        }
    }

    public ArrayList<Tipomaquina> listarTipoMaquina() {
        ArrayList<Tipomaquina> maquinas = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Tipomaquina order by idtipomaquina").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Tipomaquina d = (Tipomaquina) iter.next();
                Hibernate.initialize(d.getNombre());
                Hibernate.initialize(d.getDescripcion());
                maquinas.add(d);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return maquinas;
        }
    }

    public ArrayList<Maquina> listarMaquinaria() {
        ArrayList<Maquina> maquinas = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Maquina order by Idmaquina").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Maquina d = (Maquina) iter.next();
                Hibernate.initialize(d.getIdmaquina());
                Hibernate.initialize(d.getCodigo());
                Hibernate.initialize(d.getEstado());
                Hibernate.initialize(d.getFechaIngreso());
                Hibernate.initialize(d.getMaxProduccion());
                Hibernate.initialize(d.getTipomaquina());
                maquinas.add(d);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return maquinas;
        }
    }

    public ArrayList<Provincia> listarProvincia(Departamento d) {
        ArrayList<Provincia> provincias = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Provincia where iddepartamento = " + d.getIddepartamento() + " order by idprovincia").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Provincia p = (Provincia) iter.next();

                Hibernate.initialize(p.getIdprovincia());
                Hibernate.initialize(p.getDepartamento());
                provincias.add(p);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return provincias;
        }
    }

    public ArrayList<Departamento> listarDepartamento() {
        ArrayList<Departamento> departamentos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Departamento order by iddepartamento").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Departamento d = (Departamento) iter.next();

                Hibernate.initialize(d.getIddepartamento());

                departamentos.add(d);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return departamentos;
        }
    }

    ////////////////////////////////
    public ArrayList<Distrito> listaraAllDistrito() {
        ArrayList<Distrito> distritos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Distrito order by iddistrito").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Distrito d = (Distrito) iter.next();
                Hibernate.initialize(d.getIddistrito());
                Hibernate.initialize(d.getProvincia());
                distritos.add(d);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return distritos;
        }
    }

    public ArrayList<Provincia> listarAllProvincia() {
        ArrayList<Provincia> provincias = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Provincia order by idprovincia").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Provincia p = (Provincia) iter.next();

                Hibernate.initialize(p.getIdprovincia());
                Hibernate.initialize(p.getDepartamento());
                provincias.add(p);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return provincias;
        }
    }

    public ArrayList<Departamento> listarAllDepartamento() {
        ArrayList<Departamento> departamentos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Departamento order by iddepartamento").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Departamento d = (Departamento) iter.next();

                Hibernate.initialize(d.getIddepartamento());

                departamentos.add(d);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return departamentos;
        }
    }
    /////////////////////////////////

    /* aqui se obtiene una lista de empleados */
    public ArrayList<Empleado> listarEmpleado() {
        ArrayList<Empleado> listaResultados = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Empleado order by idempleado").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Empleado p = (Empleado) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getNombre());
                Hibernate.initialize(p.getApPaterno());
                Hibernate.initialize(p.getApMaterno());
                Hibernate.initialize(p.getCorreo());
                Hibernate.initialize(p.getTelefono());
                Hibernate.initialize(p.getCelular());
                Hibernate.initialize(p.getNumdocumento());
                Hibernate.initialize(p.getDireccion());
                Hibernate.initialize(p.getFechanac());
                Hibernate.initialize(p.getSueldo());
                Hibernate.initialize(p.getTurnoTrabajo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getTipoempleado());
                Hibernate.initialize(p.getDistrito());
                Hibernate.initialize(p.getDistrito().getProvincia());
                Hibernate.initialize(p.getDistrito().getProvincia().getDepartamento());
                Hibernate.initialize(p.getTipodocumento());
                listaResultados.add(p);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return listaResultados;
        }
    }
    
    
    public void guardarSuperHombre(Superhombre superhombre) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            session.saveOrUpdate(superhombre);
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
        }
    }
    
    public ArrayList<Empleado> listarOperarios() {
        ArrayList<Empleado> listaResultados = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Empleado where tipoempleado = 1 order by idempleado").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Empleado p = (Empleado) iter.next();
                Hibernate.initialize(p.getCodigo());
                Hibernate.initialize(p.getNombre());
                Hibernate.initialize(p.getApPaterno());
                Hibernate.initialize(p.getApMaterno());
                Hibernate.initialize(p.getCorreo());
                Hibernate.initialize(p.getTelefono());
                Hibernate.initialize(p.getCelular());
                Hibernate.initialize(p.getNumdocumento());
                Hibernate.initialize(p.getDireccion());
                Hibernate.initialize(p.getFechanac());
                Hibernate.initialize(p.getSueldo());
                Hibernate.initialize(p.getTurnoTrabajo());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getTipoempleado());
                Hibernate.initialize(p.getDistrito());
                Hibernate.initialize(p.getDistrito().getProvincia());
                Hibernate.initialize(p.getDistrito().getProvincia().getDepartamento());
                Hibernate.initialize(p.getTipodocumento());
                listaResultados.add(p);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return listaResultados;
        }
    }
}
